﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Lean.Touch;
using UnityEngine;

namespace SKS
{
    public class Token
    {
        //private float angle;
        public int Class {get; set;}
        public IList<LeanFinger> Points{get; set;}
        public Vector2 Position{get; set;}
        public int losePoint { get; set; } = -1;
        
        public bool isValid { get; set; } = false;
        public float Angle{
            get
            {
                var XAxisVector = Points[1].ScreenPosition - Points[0].ScreenPosition;
                float newAngle;
                float direction = Vector3.Cross(Vector3.right, XAxisVector).normalized.z;
                float tmpAngle = Vector3.Angle(Vector3.right, XAxisVector);

                if (direction > 0)
                    newAngle = tmpAngle;
                else
                {
                    newAngle = 180 + (180 - tmpAngle);
                }
                return newAngle;
            }
            //set => angle = value;
        }

        public float Radius {get; set;}
        
        public Token(IList<LeanFinger> points, int id)
        {
            Points = points;
            Class = id;
        }

        // public bool AddPoint(LeanFinger point)
        // {
        //     if (Points.Count<3)
        //     {
        //         Points.Add(point);
        //         return true;
        //     }
        //     return false;
        // }

        public Vector2 RuntimePosition
        {
            get
            {
                return Points.Aggregate(new Vector2(0,0),
                    (total, next) => total + (next.ScreenPosition)/3 );
            }
        }

        public string ToString()
        {
            return String.Format($"令牌{Class} :{Points[0].Index}:{Points[1].Index}:{Points[2].Index}");
        }
        
    }
}

